package com.platform.service;

import com.platform.dao.ApiBalanceMapper;
import com.platform.entity.BalanceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户余额Service实现类
 *
 * @author Rin
 * @email immane@163.com
 * @date 2018-10-10 16:26:45
 */
@Service
public class ApiBalanceService {
    @Autowired
    private ApiBalanceMapper apiBalanceMapper;

    public BalanceVo queryObject(Integer id) {
        return apiBalanceMapper.queryObject(id);
    }

    public BalanceVo queryObjectByUserId(Long id) {
        return apiBalanceMapper.queryObjectByUserId(id);
    }

    public List<BalanceVo> queryList(Map<String, Object> map) {
        return apiBalanceMapper.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return apiBalanceMapper.queryTotal(map);
    }

    public int save(BalanceVo balance) {
        return apiBalanceMapper.save(balance);
    }

    public int update(BalanceVo balance) {
        return apiBalanceMapper.update(balance);
    }

    public int delete(Integer id) {
        return apiBalanceMapper.delete(id);
    }

    public int deleteBatch(Integer[] ids) {
        return apiBalanceMapper.deleteBatch(ids);
    }

    public Map<String, Object> createBaseMap(BalanceVo balance, String type, BigDecimal amount) {
        return new HashMap<String, Object>() {{
            put("id", balance.getId());
            put("type", type);
            put(type, amount);

            // balance log
            put("user_id", balance.getUserId());

            switch (type) {
                case "balance":
                    put("before", balance.getBalance());
                    put("after", balance.getBalance().add(amount));
                    break;
                case "fund":
                    put("before", balance.getFund());
                    put("after", balance.getFund().add(amount));
                    break;
                case "point":
                    put("before", balance.getPoint());
                    put("after", balance.getPoint().add(amount));
                    break;
            }
        }};
    }

    public int increaseBalance(BalanceVo balance, BigDecimal amount) {
        Map map = createBaseMap(balance, "balance", amount);
        return apiBalanceMapper.increase(map);
    }

    public int decreaseBalance(BalanceVo balance, BigDecimal amount) {
        return increaseBalance(balance, amount.negate());
    }

    public int increaseBalance(BalanceVo balance, Integer order_id, BigDecimal amount) {
        Map map = createBaseMap(balance, "balance", amount);
        map.put("order_id", order_id);
        return apiBalanceMapper.increase(map);
    }

    public int decreaseBalance(BalanceVo balance, Integer order_id, BigDecimal amount) {
        return increaseBalance(balance, order_id, amount.negate());
    }


    public int increaseFund(BalanceVo balance, BigDecimal amount) {
        Map map = createBaseMap(balance, "fund", amount);
        return apiBalanceMapper.increase(map);
    }

    public int decreaseFund(BalanceVo balance, BigDecimal amount) {
        return increaseFund(balance, amount.negate());
    }

    public int increaseFund(BalanceVo balance, Integer order_id, BigDecimal amount) {
        Map map = createBaseMap(balance, "fund", amount);
        map.put("order_id", order_id);
        return apiBalanceMapper.increase(map);
    }

    public int decreaseFund(BalanceVo balance, Integer order_id, BigDecimal amount) {
        return increaseFund(balance, order_id, amount.negate());
    }
}
